package com.yiwei.ydd.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.ydd.R;
import com.yiwei.ydd.activity.MyCommissionDetailActivity;
import com.yiwei.ydd.activity.MyCouponActivity;
import com.yiwei.ydd.activity.MyOrderDetailActivity;
import com.yiwei.ydd.activity.MyWalletActivity;
import com.yiwei.ydd.activity.ReturnCashActivity;
import com.yiwei.ydd.api.model.MesgListModel;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyMessageAdapter extends ItemAdapter<MesgListModel.DatasBean, MyMessageAdapter.DefaultHolder> {

    private Context context;

    public MyMessageAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_message, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        MesgListModel.DatasBean item = getItem(position);
        holder.id = item.id;
        holder.txtTitle.setText(item.title);
        holder.txtContent.setText(item.content);
        if (item.remind.equals("0")) {
            holder.imgSign.setVisibility(View.GONE);
        } else {
            holder.imgSign.setVisibility(View.VISIBLE);
        }
        holder.txtTime.setText(Util.Timeformat(Long.valueOf(item.time) * 1000));
        if (item.type.equals("1")) {
            holder.btnCheck.setVisibility(View.GONE);
        } else {
            holder.btnCheck.setVisibility(View.VISIBLE);
        }
//（1：系统消息，2：钱包消息，3：订单消息，4：佣金消息，5：返现消息，6：优惠券消息）

        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (item.type) {
                    case "1":

                        break;
                    case "2":
                        Util.startActivity(context, MyWalletActivity.class);
                        break;
                    case "3":
                        Intent intent = new Intent(context, MyOrderDetailActivity.class);
                        intent.putExtra("order_number", item.notify_key);
                        context.startActivity(intent);
                        ((BaseActivity)context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
//                        String order_number = getIntent().getStringExtra("order_number");
                        break;
                    case "4":
                        Util.startActivity(context, MyCommissionDetailActivity.class);
                        break;
                    case "5":
                        Util.startActivity(context, ReturnCashActivity.class);
                        break;
                    case "6":
                        Util.startActivity(context, MyCouponActivity.class);
                        break;
                }
            }
        });

    }


    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.btn_check)
        LinearLayout btnCheck;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        @BindView(R.id.img_sign)
        SuperButton imgSign;
        @BindView(R.id.btn_delete)
        TextView btnDelete;
        private String id;

        @OnClick({R.id.btn_delete, R.id.layout_total})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_delete:
                    if (onChooseListener != null) {
                        onChooseListener.onDelete(id);
                    }
                    break;
                case R.id.layout_total:
                    if (onChooseListener != null) {
                        onChooseListener.onClick(id);
                    }
                    break;
            }
        }

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String id);

        void onDelete(String id);
    }

    public String allIds() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < getmSource().size(); i++) {
            builder.append(getmSource().get(i).id);
            builder.append(",");
        }
        if (builder.length() != 0) {
            return builder.substring(0, builder.length() - 1);
        } else {
            return "";
        }
    }
}
